<?php
class BrowseController extends SilkControllerBase {
	
	public function index($params) {
//		echo "<pre>"; var_dump($params); echo "</pre>";

		if( !isset( $params["browse"] )) {
			$params["browse"] = "";
		}
		// setup menu items
		$usertypes = array(1 => "Model", 2 => "Photographer", 3 => "Make-Up Artist");
		$this->set("usertypes", $usertypes);
		
		$experiences = orm("experience")->find_all_by_status_id("1");
		$active_experiences = array();
		foreach( $experiences as $experience ) {
			$active_experiences[$experience->id] = $experience->name;
		}
		
		$this->set("experiences", $active_experiences);
		$this->set("genders", array("M" => "Male", "F" => "Female"));
		$this->set("params", $params);
		
		$ages = array("Select Age", "15-19", "20-24", "25-29", "30-34", "35-39", "40-100");
		$this->set("ages", $ages);
		
		// setup filter items when shortcut link is clicked
		if( !isset( $params["input"] )) {
			if( $params["browse"] == "usertype" ) {
				$this->set("selected_usertype_id", $params["id"]);
				$params["usertype_id"] = $params["id"];
			}
			
			if( $params["browse"] == "experience" ) {
				$experiences = orm("experience")->find_all_by_status_id("1");
				foreach( $experiences as $experience ) {
					if( $experience->id == $params["id"] ) {
						$this->set("selected_experience_id", $experience->id);
						$params["experience_id"] = $experience->id;
						break;
					}
				}
			}
			
			if( $params["browse"] == "gender" ) {
				$this->set("selected_gender", $params["id"]);
				$params["gender"] = $params["id"];
			}
			
			if( $params["browse"] == "age" ) {
				$this->set("selected_age", $params["id"]);
				$params["age"] = $params["id"];
			}
		}
		
		// setup filter items and build sql
		$conditions = array("p.status_id = 1");
		
		if( isset( $params["usertype_id"] ) && $params["usertype_id"] != 0 ) {
			$conditions[] = "p.type = '" . str_replace(" ", "", str_replace("-", "", $usertypes[$params["usertype_id"]])) . "Portfolio'";
			$this->set("selected_usertype_id", $params["usertype_id"]);
		}
		if( isset( $params["experience_id"] ) && $params["experience_id"] != 0 ) {
			echo "<pre>setting experience to $params[experience_id]</pre>";
			$conditions[] = "p.experience_id = " . $params["experience_id"];
			$this->set("selected_experience_id", $params["experience_id"]);
		}
		if( isset( $params["gender"] ) && strlen($params["gender"]) == 1 ) {
			$conditions[] = "u.gender = '" . $params["gender"] . "'";
			$this->set("selected_gender", $params["gender"]);
		}
		if( isset( $params["age"] ) && $params["age"] > 0 ) {
			list($low_age, $high_age) = explode("-", $ages[$params["age"]]);
			list($low_dob, $high_dob) = $this->find_dates_by_age($low_age, $high_age);
			$conditions[] = "u.date_of_birth BETWEEN '$low_dob' AND '$high_dob'";
			$this->set("selected_age", $params["age"]);
		}
		
		$select = "SELECT p.* FROM " . db_prefix() . "portfolios AS p";
		$select.= " INNER JOIN " . db_prefix() . "users AS u ON u.id=p.user_id";
		if( count( $conditions )) {
			$select.= " WHERE";
			foreach( $conditions as $condition ) {
				$select.= " $condition AND";
			}
			$select = substr( $select, 0, strlen( $select ) - 4);
			$select.= " ORDER BY p.type";
//			$this->set("select", $select);
		}
		$results = orm("portfolio")->find_all_by_query($select);
//		echo "<pre>"; var_dump($results); echo "</pre>";
		$this->set("results", $results);
	}
	
	/**
	 * Calculate the DOB's we want to look for based on the age
	 *
	 * @param unknown_type $low_age
	 * @param unknown_type $high_age
	 */
	private function find_dates_by_age($low_age, $high_age) {
		$high_age = $high_age + 1;
		$low_dob = strftime("%Y-%m-%d", strtotime("-$high_age years +1 days"));
		
		$low_age = $low_age + 1;
		$high_dob = strftime("%Y-%m-%d", strtotime("-$low_age years +1 days"));
		
//		echo "today: $current_date low dob: $low_dob high dob: $high_dob<br />";
		return array($low_dob, $high_dob);
	}
	
	public function experience($params) {
		$selected_experience = orm("experience")->find_by_name($params["experience"]);
		$this->set("selected_experience_id", $selected_experience->id);
//		echo "<pre>"; var_dump($params); echo "</pre>";
	}
	
	public function add_friend($params) {
		echo "<pre>"; var_dump($params); echo "</pre>";
		$date = db()->DBTimeStamp(time());
		db()->Execute("INSERT INTO silk_friends (user_id, friend_id, status, create_date, modified_date) VALUES (?,?,?,{$date},{$date})", array($_SESSION["silk_user"]["id"], $params["friend_id"], 1));
		redirect($_SERVER["HTTP_REFERER"]);
	}
	
}